package myfirstproject.firstproject.Controller;


import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import myfirstproject.firstproject.Service.IArticleService;
import myfirstproject.firstproject.Service.IUserService;
import myfirstproject.firstproject.domain.DTO.ArticleDTO;
import myfirstproject.firstproject.domain.DTO.ArticleQueryDTO;
import myfirstproject.firstproject.domain.DTO.ArticleTitleDTO;
import myfirstproject.firstproject.domain.DTO.UserGiveUpDTO;
import myfirstproject.firstproject.domain.VO.ArticleUserVO;
import myfirstproject.firstproject.domain.VO.ArticleVO;
import myfirstproject.firstproject.domain.VO.UserVo;
import myfirstproject.firstproject.domain.po.Article;
import myfirstproject.firstproject.result.PageResult;
import myfirstproject.firstproject.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-09-24
 */
@RestController
@Tag(name="文章管理")
@RequestMapping("/user/article")
@RequiredArgsConstructor
@Slf4j
public class ArticleController {
    @Autowired
    private IArticleService articleService;
    @Autowired
    private IUserService userService;

    /**
     * 新增文章
     * @param articleDTO
     * @return
     */
    @Operation(summary = "新增文章接口")
    @PostMapping("/save")
    public Result saveArticle(@RequestBody ArticleDTO articleDTO){

        //新增文章
        articleService.saveArticle(articleDTO);
        return Result.success();
    }

    /**
     * 删除文章
     * @param id
     * @return
     */
    @Operation(summary = "删除文章")
    @DeleteMapping("/delete/{id}")
    public Result deleteArticle(@PathVariable String id){
        articleService.removeById(id);
        return Result.success();
    }

    /**
     * 修改文章标题
     * @param articleTitleDTO
     * @return
     */
    @Operation(summary = "修改文章标题")
    @PostMapping("/update")
    public Result updateArticle(@RequestBody ArticleTitleDTO articleTitleDTO){
        articleService.updateArticleTitle(articleTitleDTO);
        return Result.success();
    }

    @Operation(summary = "文章分页查询")
    @PostMapping("/page")
    public PageResult<ArticleVO> queryArticlesPage(@RequestBody ArticleQueryDTO query){
        return articleService.queryArticlesPage(query);
    }
    @Operation(summary = "文章详情查询")
    @GetMapping("/detail")
    public Result<ArticleVO> queryArticleDetail(String id){
        Article article = articleService.getById(id);
        ArticleVO articleVO = BeanUtil.copyProperties(article, ArticleVO.class);
        UserVo userVo = userService.queryUserAndAddressById(article.getPublisher());
        ArticleUserVO articleUserVO = BeanUtil.copyProperties(userVo, ArticleUserVO.class);
        articleVO.setArticleUserVO(articleUserVO);
        return Result.success(articleVO);
    }


    @Operation(summary = "文章点赞")
    @PostMapping("/giveUp")
    public Result giveUp(@RequestBody UserGiveUpDTO userGiveUpDTO){
        articleService.addGiveUp(userGiveUpDTO);
        return Result.success();
    }
    @Operation(summary = "取消文章点赞")
    @DeleteMapping("/cancelGiveUp")
    public Result cancelGiveUp(@RequestBody UserGiveUpDTO userGiveUpDTO){
        articleService.cancelGiveUp(userGiveUpDTO);
        return Result.success();
    }

}
